package com.ruoyi.ruoyictmanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.ruoyictmanage.domain.entity.*;
import com.ruoyi.ruoyictmanage.mapper.LineMapper;
import com.ruoyi.ruoyictmanage.mapper.MPNMapper;
import com.ruoyi.ruoyictmanage.service.MPNService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MPNServiceimpl implements MPNService {

    @Autowired
    private MPNMapper mpnMapper;

    @Autowired
    private LineMapper lineMapper;

    @Override
    public PageInfo<MPN> getMPNlist(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<MPN> mpnList=mpnMapper.getMPNlist();
        PageInfo<MPN> pageInfo=new PageInfo<>(mpnList);
        return pageInfo;
    }

    @Override
    public List<Line> getLineList() {
        return lineMapper.getLineList();
    }

    @Override
    public List<LinkUser> UserIdUserNameByDeptId(Long deptId) {
        return lineMapper.UserIdUserNameByDeptId(deptId);
    }

    @Override
    public List<Integer> getUserLinkLine(Long userId) {
        return lineMapper.getUserLinkLine(userId);
    }

    @Override
    public int setUserLinkLine(RequestDat requestDat) {
        lineMapper.updateLineLinkState(requestDat.getLineId(),requestDat.getUserId());
        return 0;
    }

    @Override
    public int addMPN(MPN mpn) {
        return mpnMapper.addMPN(mpn);
    }

    @Override
    public List<Line> getLineListByLoginId(Long userId) {
        return lineMapper.getLineListByLoginId(userId);
    }

    @Override
    public List<MPNCTDTO> getCTTableDBByLineIdEnterDateMPNName(getCTDTO ctdto) {
        List<Integer> line = ctdto.getLine();
        String timeStar=ctdto.getTimeStar();
        String timeEnd = ctdto.getTimeEnd();
        String mpnName = ctdto.getMpnName();
        Integer state = ctdto.getState();

        return mpnMapper.getCTTableDBByLineIdEnterDateMPNName(line, timeStar, timeEnd, mpnName,state);
    }

    @Override
    public PageInfo<MPNCTDTO> getCTTableByLineIdEnterDateMPNNameHaveData(getCTDTO ctdto) {
        List<Integer> line = ctdto.getLine();
        String timeStar=ctdto.getTimeStar();
        String timeEnd = ctdto.getTimeEnd();
        String mpnName = ctdto.getMpnName();
        String ie =ctdto.getIe();
        int pageSize=ctdto.getPageSize();
        PageHelper.startPage(ctdto.getPageNum(),pageSize);
        List<MPNCTDTO> ctTableByLineIdEnterDateMPNName = mpnMapper.getCTTableByLineIdEnterDateMPNNameHaveData(line, timeStar, timeEnd, mpnName,ie);
        return new PageInfo<>(ctTableByLineIdEnterDateMPNName);
    }

    @Override
    public List<MPNCTDTO> getCTTableByCTId(Long ctId) {
        return mpnMapper.getCTTableByCTId(ctId);
    }

    @Override
    public List<MPN> getMPNlistByQueryParams(String mpnName, String ie, Integer lineId, Integer state, String timeStar, String timeEnd) {
       return mpnMapper.getMPNlistByQueryParams(mpnName,lineId,state,timeStar,timeEnd,ie);
    }

    @Override
    public PageInfo<MPNCTDTO> getCTTableByLineIdEnterDateMPNName(getCTDTO ctdto,boolean excelB) {
        List<Integer> line = ctdto.getLine();
        String timeStar=ctdto.getTimeStar();
        String timeEnd = ctdto.getTimeEnd();
        String mpnName = ctdto.getMpnName();
        String ie =ctdto.getIe();
        int pageSize=ctdto.getPageSize();
        if (excelB){
            pageSize=2500;
        }
        PageHelper.startPage(ctdto.getPageNum(),pageSize);
        List<MPNCTDTO> ctTableByLineIdEnterDateMPNName = mpnMapper.getCTTableByLineIdEnterDateMPNName(line, timeStar, timeEnd, mpnName,ie);
        return new PageInfo<>(ctTableByLineIdEnterDateMPNName);
    }

    @Override
    public PageInfo<MPN> getMPNlistByQueryParams(int pageNum, int pageSize, String mpnName,String ie, int lineId, int state, String timeStar, String timeEnd) {
        PageHelper.startPage(pageNum,pageSize);
        System.err.println(ie);
        List<MPN> mpnList=mpnMapper.getMPNlistByQueryParams(mpnName,lineId,state,timeStar,timeEnd,ie);
        return new PageInfo<>(mpnList);
    }

    @Override
    public int setMPN(MPN mpn) {
        return mpnMapper.setMPN(mpn);
    }

    @Override
    public void batchSave(List<MPN> dataList) {
        if (CollectionUtils.isEmpty(dataList)) {
            return;
        }

        // 分批处理
        int batchSize = 500;
        for (int i = 0; i < dataList.size(); i += batchSize) {
            List<MPN> subList = dataList.subList(i, Math.min(i + batchSize, dataList.size()));
            mpnMapper.batchSave(subList);
        }
    }

    @Override
    public Long setMpnState(Long id, int state) {
        return mpnMapper.setMpnState(id,state);
    }


    @Override
    public int setAddCTtable(CT ct) {
        return mpnMapper.setAddCTtable(ct);
    }

    @Override
    public int setUpdateCTtable(CT ct) {
        return mpnMapper.setUpdateCTtable(ct);
    }

    @Override
    public void batchSaveCT(List<CT> dataList) {
        mpnMapper.batchSaveCT(dataList);
    }

    @Override
    public int delct(Long id) {
        System.err.println(id);
        return mpnMapper.delct(id);
    }

    @Override
    public int setBatchMpnState(List<List<String>> multipleSelection) {
        for (List<String> strings : multipleSelection) {
            mpnMapper.setMpnState(Long.valueOf(strings.get(0)), Integer.parseInt(strings.get(1)));
        }
        return 0;
    }




}
